package com.duan.laugh.mp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.mp.api.pojo.entity.MpAccountFans;
import com.duan.laugh.mp.service.IMpAccountFansService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 微信公众号粉丝管理
 *
 * @author duanjw
 * @date 2019-03-26 22:08:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wxaccountfans")
public class MpAccountFansController {

    private final IMpAccountFansService IMpAccountFansService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param wxAccountFans 微信公众号粉丝
     * @return
     */
    @GetMapping("/page")
    public R getWxAccountFansPage(Page page, MpAccountFans wxAccountFans) {
        return R.ok(IMpAccountFansService.page(page, Wrappers.query(wxAccountFans)));
    }


    /**
     * 通过id查询微信公众号粉丝
     *
     * @param id id
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(IMpAccountFansService.getById(id));
    }

    /**
     * 新增微信公众号粉丝
     *
     * @param wxAccountFans 微信公众号粉丝
     * @return R
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('mp_wxaccountfans_add')")
    public R save(@RequestBody MpAccountFans wxAccountFans) {
        return R.ok(IMpAccountFansService.save(wxAccountFans));
    }

    /**
     * 修改微信公众号粉丝
     *
     * @param wxAccountFans 微信公众号粉丝
     * @return R
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('mp_wxaccountfans_edit')")
    public R updateById(@RequestBody MpAccountFans wxAccountFans) {
        return R.ok(IMpAccountFansService.updateById(wxAccountFans));
    }

    /**
     * 通过id删除微信公众号粉丝
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mp_wxaccountfans_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(IMpAccountFansService.removeById(id));
    }

    /**
     * 同步粉丝
     *
     * @param appId
     * @return
     */
    @PostMapping("/sync/{appId}")
    public R sync(@PathVariable String appId) {
        return R.ok(IMpAccountFansService.syncAccountFans(appId));
    }

}
